package com.itheima.controller;

import com.alibaba.excel.EasyExcel;
import com.itheima.Utils.RoleCreate;
import com.itheima.Utils.Transaction;
import com.itheima.domain.QueryByGender;
import com.itheima.domain.Role;
import com.itheima.domain.TurnPage;
import com.itheima.listener.ExcelListener;
import com.itheima.service.RoleService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.util.List;

import com.itheima.Utils.MultipartFileToFile;

@RestController
@RequestMapping("/item")
public class UserController implements ApplicationContextAware {

    private ApplicationContext context;

    @Autowired
    private RoleService roleService;



    @RequestMapping(value = "/page{page}")
    public ModelAndView save(@PathVariable(value = "page")int page) throws InterruptedException {

        TurnPage turnPage = new TurnPage();

        turnPage.setStartIndex((page-1) * 10);

        turnPage.setPageSize(10);

        ModelAndView modelAndView = new ModelAndView();

        Thread.sleep(500);

        modelAndView.addObject("roleList", roleService.turnPages(turnPage));

        modelAndView.addObject("page", page);

        modelAndView.setViewName("creat");
        if (roleService.countId() % 10 == 0){
            modelAndView.addObject("total",roleService.countId() / 10);
        }

        else {
            modelAndView.addObject("total",roleService.countId() / 10 + 1);
        }

        return modelAndView;
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public ModelAndView save1(String rolename, String telephone, String gender, String department){
//        System.out.println(rolename + telephone + gender + department);
        Role role = RoleCreate.create(rolename, gender,department,telephone);
        roleService.SaveRole(role);
        ModelAndView modelAndView = new ModelAndView();
        if (roleService.countId() % 10 == 0){
            modelAndView.addObject("page",roleService.countId() / 10);
            modelAndView.addObject("total",roleService.countId() / 10);
        }

        else {
            modelAndView.addObject("page",roleService.countId() / 10 + 1);
            modelAndView.addObject("total",roleService.countId() / 10 + 1);
        }
        modelAndView.setViewName("redirect:/item/page{page}");

        return modelAndView;
    }


    @RequestMapping(value = "/saveByExcel")
    @ResponseBody
    public String saveByExcel(MultipartFile multipartFile) throws Exception {

        String fileName = multipartFile.getOriginalFilename();
        System.out.println(fileName);
        File file = null;
        try {
            //将MultipartFile转为File
            file = MultipartFileToFile.multipartFileToFile(multipartFile);
            ExcelListener excelListener = new ExcelListener();
            EasyExcel.read(file,Role.class,excelListener).sheet().doRead();
            getTransaction().commit(excelListener.getList());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MultipartFileToFile.deleteTempFile(file);
        }
        return "{\"result\":\"200\"}";
    }

    @RequestMapping(value = "/deleteById")
    @ResponseBody
    public ModelAndView deleteById(ModelAndView modelAndView, int id){
        roleService.DeleteById(id);
        modelAndView.setViewName("creat");
        if (roleService.countId() % 10 == 0){
            modelAndView.addObject("total",roleService.countId() / 10);
        }

        else {
            modelAndView.addObject("total",roleService.countId() / 10 + 1);
        }
        return modelAndView;
    }

    @RequestMapping(value = "/deleteByIdEasy")
    @ResponseBody
    public String deleteByIdEasy(int id){
        roleService.DeleteById(id);
        return "{\"result\":\"200\"}";
    }

    @RequestMapping(value = "/deleteByName")
    @ResponseBody
    public ModelAndView deleteByName(ModelAndView modelAndView, String rolename){
        roleService.DeleteByName(rolename);
        modelAndView.setViewName("creat");
        if (roleService.countId() % 10 == 0){
            modelAndView.addObject("total",roleService.countId() / 10);
        }

        else {
            modelAndView.addObject("total",roleService.countId() / 10 + 1);
        }
        return modelAndView;
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public ModelAndView save3(ModelAndView modelAndView,int id, double amount,double deduction,double salary){
        Role role = RoleCreate.update(id,amount,deduction,salary);
        roleService.UpdateById(role);
        List<Role> roleList = roleService.findById(id);
        modelAndView.addObject("roleList", roleList);
        modelAndView.setViewName("creat");
        if (roleService.countId() % 10 == 0){
            modelAndView.addObject("total",roleService.countId() / 10);
        }

        else {
            modelAndView.addObject("total",roleService.countId() / 10 + 1);
        }
        return modelAndView;
    }

    @RequestMapping(value = "/queryById")
    @ResponseBody
    public ModelAndView queryById(int id){

        List<Role> roleList = roleService.findById(id);
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("roleList", roleList);
        modelAndView.setViewName("creat");
        if (roleService.countId() % 10 == 0){
            modelAndView.addObject("total",roleService.countId() / 10);
        }

        else {
            modelAndView.addObject("total",roleService.countId() / 10 + 1);
        }

        return modelAndView;
    }

    @RequestMapping(value = "/RankBySalary{page}")
    @ResponseBody
    public ModelAndView RankBySalary(@PathVariable(value = "page")int page){

        TurnPage turnPage = new TurnPage();

        turnPage.setStartIndex((page-1) * 10);

        turnPage.setPageSize(10);


        List<Role> rankBySalary = roleService.RankBySalary(turnPage);
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("roleList", rankBySalary);
        modelAndView.setViewName("RankBySalary");
        if (roleService.countId() % 10 == 0){
            modelAndView.addObject("total",roleService.countId() / 10);
        }

        else {
            modelAndView.addObject("total",roleService.countId() / 10 + 1);
        }

        return modelAndView;
    }

    @RequestMapping(value = "/queryGender{page}")
    @ResponseBody
    public ModelAndView queryGender(String gender, @PathVariable(value = "page")int page){

        QueryByGender queryByGender = new QueryByGender();
        queryByGender.setGender(gender);
        queryByGender.setPageSize(10);
        queryByGender.setStartIndex((page-1) * 10);
        List<Role> roleList = roleService.findByGender(queryByGender);
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("roleList", roleList);
        modelAndView.addObject("gender", gender);
        modelAndView.setViewName("queryByGender");
        if (roleService.countGender(gender) % 10 == 0){
            modelAndView.addObject("total",roleService.countGender(gender) / 10);
        }

        else {
            modelAndView.addObject("total",roleService.countGender(gender) / 10 + 1);
        }

        return modelAndView;
    }


    public Transaction getTransaction() {
        return this.context.getBean(Transaction.class);
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}
